package com.jack.cms.modular.system.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jack.cms.common.annotion.Permission;
import com.jack.cms.common.annotion.log.BussinessLog;
import com.jack.cms.common.constant.Dict;
import com.jack.cms.common.constant.factory.ConstantFactory;
import com.jack.cms.common.controller.BaseController;
import com.jack.cms.common.exception.BizExceptionEnum;
import com.jack.cms.common.exception.BussinessException;
import com.jack.cms.common.node.ZTreeNode;
import com.jack.cms.common.persistence.dao.DeptMapper;
import com.jack.cms.common.persistence.model.Dept;
import com.jack.cms.core.log.LogObjectHolder;
import com.jack.cms.core.util.ToolUtil;
import com.jack.cms.modular.system.dao.DeptDao;
import com.jack.cms.modular.system.warpper.DeptWarpper;

/**
 * 部门控制器
 *
 * @author JackYuan
 * @Date 2017年2月17日20:27:22
 */
@Controller
@RequestMapping("/dept")
public class DeptController extends BaseController {

	private String PREFIX = "/system/dept/";

	@Resource
	DeptDao deptDao;

	@Resource
	DeptMapper deptMapper;

	/**
	 * 跳转到部门管理首页
	 */
	@RequestMapping("")
	public String index() {
		return PREFIX + "dept.html";
	}

	/**
	 * 跳转到添加部门
	 */
	@RequestMapping("/dept_add")
	public String deptAdd() {
		return PREFIX + "dept_add.html";
	}

	/**
	 * 跳转到修改部门
	 */
	@Permission
	@RequestMapping("/dept_update/{deptId}")
	public String deptUpdate(@PathVariable Integer deptId, Model model) {
		Dept dept = deptMapper.selectById(deptId);
		model.addAttribute(dept);
		model.addAttribute("pName", ConstantFactory.me().getDeptName(dept.getPid()));
		LogObjectHolder.me().set(dept);
		return PREFIX + "dept_edit.html";
	}

	/**
	 * 获取部门的tree列表
	 */
	@RequestMapping(value = "/tree")
	@ResponseBody
	public List<ZTreeNode> tree() {
		List<ZTreeNode> tree = this.deptDao.tree();
		tree.add(ZTreeNode.createParent());
		return tree;
	}

	/**
	 * 新增部门
	 */
	@BussinessLog(value = "添加部门", key = "simplename", dict = Dict.DeptDict)
	@RequestMapping(value = "/add")
	@Permission
	@ResponseBody
	public Object add(Dept dept) {
		if (ToolUtil.isOneEmpty(dept, dept.getSimplename())) {
			throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
		}
		return this.deptMapper.insert(dept);
	}

	/**
	 * 获取所有部门列表
	 */
	@RequestMapping(value = "/list")
	@Permission
	@ResponseBody
	public Object list(String condition) {
		List<Map<String, Object>> list = this.deptDao.list(condition);
		return new DeptWarpper(list).warpList();
	}

	/**
	 * 部门详情
	 */
	@RequestMapping(value = "/detail/{deptId}")
	@Permission
	@ResponseBody
	public Object detail(@PathVariable("deptId") Integer deptId) {
		return deptMapper.selectById(deptId);
	}

	/**
	 * 修改部门
	 */
	@BussinessLog(value = "修改部门", key = "simplename", dict = Dict.DeptDict)
	@RequestMapping(value = "/update")
	@Permission
	@ResponseBody
	public Object update(Dept dept) {
		if (ToolUtil.isEmpty(dept) || dept.getId() == null) {
			throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
		}
		deptMapper.updateById(dept);
		return SUCCESS_TIP;
	}

	/**
	 * 删除部门
	 */
	@BussinessLog(value = "删除部门", key = "deptId", dict = Dict.DeleteDict)
	@RequestMapping(value = "/delete")
	@Permission
	@ResponseBody
	public Object delete(@RequestParam Integer deptId) {

		// 缓存被删除的部门名称
		LogObjectHolder.me().set(ConstantFactory.me().getDeptName(deptId));

		deptMapper.deleteById(deptId);

		return SUCCESS_TIP;
	}
}
